package qldc.pm.tiensu.service.persistence;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import qldc.pm.tiensu.constants.TrangThaiXuLyHoSoXoaAnTichEnum;
import qldc.pm.tiensu.model.LichSuXoaAnTich;
import qldc.pm.tiensu.model.impl.LichSuXoaAnTichImpl;

import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.SQLQuery;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.dao.orm.Type;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
import com.liferay.util.dao.orm.CustomSQLUtil;

public class LichSuXoaAnTichFinderImpl extends
		BasePersistenceImpl<LichSuXoaAnTich> implements LichSuXoaAnTichFinder {
	private static Log _log = LogFactoryUtil
			.getLog(LichSuXoaAnTichFinderImpl.class);

	public static final String COUNT_BY_HOTEN_MAHOSO_NGAYLAP_TRANGTHAI = LichSuXoaAnTichFinder.class
			.getName() + ".countByHoTen_MaSoHoSo_NgayLapHoSo_TrangThai";
	public static final String FIND_BY_HOTEN_MAHOSO_NGAYLAP_TRANGTHAI = LichSuXoaAnTichFinder.class
			.getName() + ".findByHoTen_MaSoHoSo_NgayLapHoSo_TrangThai";
	public static final String FIND_BY_NGAY_KY_DUYET_XOA_AN_TICH = LichSuXoaAnTichFinder.class
			.getName() + ".findBy_NgayKyDuyetXoaAnTich";

	@SuppressWarnings("unchecked")
	public int countByHoTen_MaSoHoSo_NgayLapHoSo_TrangThai(
			String hoVaTenDoiTuongTienSu, String maHoSoXoaAnTich,
			Date ngayYeuCauXoaAnTich, int trangThaiXuLyHoSoXoaAnTich)
			throws SystemException {
		Session session = null;

		try {
			session = openSession();

			String sql = CustomSQLUtil
					.get(COUNT_BY_HOTEN_MAHOSO_NGAYLAP_TRANGTHAI);

			if (Validator.isNull(hoVaTenDoiTuongTienSu)) {
				sql = StringUtil
						.replace(
								sql,
								"AND (TS_LichSuXoaAnTich.hoVaTenDoiTuongTienSu LIKE ?)",
								StringPool.BLANK);
			}
			if (Validator.isNull(maHoSoXoaAnTich)) {
				sql = StringUtil.replace(sql,
						"AND (TS_LichSuXoaAnTich.maHoSoXoaAnTich LIKE ?)",
						StringPool.BLANK);
			}
			if (Validator.isNull(ngayYeuCauXoaAnTich)) {
				sql = StringUtil.replace(sql,
						"AND (TS_LichSuXoaAnTich.ngayYeuCauXoaAnTich =  ?)",
						StringPool.BLANK);
			}
			if (trangThaiXuLyHoSoXoaAnTich == TrangThaiXuLyHoSoXoaAnTichEnum.DOI_KY_DUYET
					.getValue()) {
				sql = StringUtil
						.replace(sql, "AND NGUOI_THUC_HIEN_XOA",
								" AND (TS_LichSuXoaAnTich.nguoiThucHienXoaAnTichTrongDuLieuID = 0)");

			} else if (trangThaiXuLyHoSoXoaAnTich == TrangThaiXuLyHoSoXoaAnTichEnum.DA_XOA_AN_TICH
					.getValue()) {
				sql = StringUtil
						.replace(sql, "AND NGUOI_THUC_HIEN_XOA",
								" AND (TS_LichSuXoaAnTich.nguoiThucHienXoaAnTichTrongDuLieuID > 0)");
			} else {
				sql = StringUtil.replace(sql, "AND NGUOI_THUC_HIEN_XOA", "");
			}
			sql = CustomSQLUtil.replaceAndOperator(sql, true);
			SQLQuery q = session.createSQLQuery(sql);
			q.addScalar(COUNT_COLUMN_NAME, Type.LONG);

			QueryPos qpos = QueryPos.getInstance(q);

			if (Validator.isNotNull(hoVaTenDoiTuongTienSu)) {
				qpos.add(StringPool.PERCENT + hoVaTenDoiTuongTienSu
						+ StringPool.PERCENT);

			}
			if (Validator.isNotNull(maHoSoXoaAnTich)) {
				qpos.add(StringPool.PERCENT + maHoSoXoaAnTich
						+ StringPool.PERCENT);

			}
			if (Validator.isNotNull(ngayYeuCauXoaAnTich)) {
				qpos.add(ngayYeuCauXoaAnTich);
			}

			Iterator<Long> iterator = q.list().iterator();

			if (iterator.hasNext()) {
				Long count = iterator.next();

				if (Validator.isNotNull(count)) {
					return count.intValue();
				}
			}

			return 0;
		} catch (Exception e) {
			throw new SystemException(e);
		} finally {
			closeSession(session);
		}
	}

	@SuppressWarnings("unchecked")
	public List<LichSuXoaAnTich> findByHoTen_MaSoHoSo_NgayLapHoSo_TrangThai(
			String hoVaTenDoiTuongTienSu, String maHoSoXoaAnTich,
			Date ngayYeuCauXoaAnTich, int trangThaiXuLyHoSoXoaAnTich,
			int start, int end, OrderByComparator obc) throws SystemException {
		Session session = null;

		try {
			session = openSession();

			String sql = CustomSQLUtil
					.get(FIND_BY_HOTEN_MAHOSO_NGAYLAP_TRANGTHAI);

			if (Validator.isNull(hoVaTenDoiTuongTienSu)) {
				sql = StringUtil
						.replace(
								sql,
								"AND (TS_LichSuXoaAnTich.hoVaTenDoiTuongTienSu LIKE ?)",
								StringPool.BLANK);
			}
			if (Validator.isNull(maHoSoXoaAnTich)) {
				sql = StringUtil.replace(sql,
						"AND (TS_LichSuXoaAnTich.maHoSoXoaAnTich LIKE ?)",
						StringPool.BLANK);
			}
			if (Validator.isNull(ngayYeuCauXoaAnTich)) {
				sql = StringUtil.replace(sql,
						"AND (TS_LichSuXoaAnTich.ngayYeuCauXoaAnTich =  ?)",
						StringPool.BLANK);
			}

			if (trangThaiXuLyHoSoXoaAnTich == TrangThaiXuLyHoSoXoaAnTichEnum.DOI_KY_DUYET
					.getValue()) {
				sql = StringUtil
						.replace(sql, "AND NGUOI_THUC_HIEN_XOA",
								" AND (TS_LichSuXoaAnTich.nguoiThucHienXoaAnTichTrongDuLieuID = 0)");

			} else if (trangThaiXuLyHoSoXoaAnTich == TrangThaiXuLyHoSoXoaAnTichEnum.DA_XOA_AN_TICH
					.getValue()) {
				sql = StringUtil
						.replace(sql, "AND NGUOI_THUC_HIEN_XOA",
								" AND (TS_LichSuXoaAnTich.nguoiThucHienXoaAnTichTrongDuLieuID > 0)");
			} else {
				sql = StringUtil.replace(sql, "AND NGUOI_THUC_HIEN_XOA", "");
			}

			sql = CustomSQLUtil.replaceAndOperator(sql, true);

			sql = CustomSQLUtil.replaceOrderBy(sql, obc);

			SQLQuery q = session.createSQLQuery(sql);

			q.addEntity("LichSuXoaAnTich", LichSuXoaAnTichImpl.class);

			QueryPos qpos = QueryPos.getInstance(q);

			if (Validator.isNotNull(hoVaTenDoiTuongTienSu)) {
				qpos.add(StringPool.PERCENT + hoVaTenDoiTuongTienSu
						+ StringPool.PERCENT);

			}

			if (Validator.isNotNull(maHoSoXoaAnTich)) {
				qpos.add(StringPool.PERCENT + maHoSoXoaAnTich
						+ StringPool.PERCENT);

			}

			if (Validator.isNotNull(ngayYeuCauXoaAnTich)) {
				qpos.add(ngayYeuCauXoaAnTich);
			}

			return (List<LichSuXoaAnTich>) QueryUtil.list(q, getDialect(),
					start, end);
		} catch (Exception e) {
			throw new SystemException(e);
		} finally {
			closeSession(session);
		}
	}

	@SuppressWarnings("unchecked")
	public List<Date> findByNgayKyDuyetXoaAnTich(
			Date tuNgayXoaAnTich, Date denNgayXoaAnTich, long doiTuongTienSuId) throws SystemException {
		Session session = null;

		try {
			session = openSession();

			String sql = CustomSQLUtil
					.get(FIND_BY_NGAY_KY_DUYET_XOA_AN_TICH);

			if (Validator.isNull(tuNgayXoaAnTich)) {
				sql = StringUtil.replace(sql,
						"AND (TS_LichSuXoaAnTich.ngayYeuCauXoaAnTich >= ?)",
						StringPool.BLANK);
			}

			if (Validator.isNull(denNgayXoaAnTich)) {
				sql = StringUtil.replace(sql,
						"AND (TS_LichSuXoaAnTich.ngayYeuCauXoaAnTich <= ?)",
						StringPool.BLANK);
			}
			if (Validator.isNull(doiTuongTienSuId)) {
				sql = StringUtil.replace(sql,
						"AND (TS_TienSu.doiTuongTienSuId = ?)",
						StringPool.BLANK);
			}

			SQLQuery q = session.createSQLQuery(sql);

			q.addScalar("ngayKyDuyetXoaAnTich", Type.DATE);

			QueryPos qpos = QueryPos.getInstance(q);

			if (Validator.isNotNull(tuNgayXoaAnTich)) {
				qpos.add(tuNgayXoaAnTich);
			}

			if (Validator.isNotNull(denNgayXoaAnTich)) {
				qpos.add(denNgayXoaAnTich);
			}
			if (Validator.isNotNull(doiTuongTienSuId)) {
				qpos.add(doiTuongTienSuId);
			}

			return (List<Date>) q.list();
		} catch (Exception e) {
			throw new SystemException(e);
		} finally {
			closeSession(session);
		}
	}
}
